#include "bin_manual.h"

BinarizationManual::BinarizationManual(PNM* img) :
    Transformation(img)
{
}

BinarizationManual::BinarizationManual(PNM* img, ImageViewer* iv) :
    Transformation(img, iv)
{
}

PNM* BinarizationManual::transform()
{
    int threshold = getParameter("threshold").toInt();

    int width  = image->width();
    int height = image->height();

    PNM* newImage = new PNM(width, height, QImage::Format_Mono);

	if (image->format() == QImage::Format_Indexed8)
	{
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < height; j++)
			{
				QRgb pixel = image->pixel(i,j);
				if(qGray(pixel) <= threshold) newImage->setPixel(i, j, 0);
				else newImage->setPixel(i, j, 1);
			}
		}
	}
	else {
		qDebug() << Q_FUNC_INFO << "Image should be gray scaled";
	}


    return newImage;
}




